package com.jjy.tools.validation.impl;

import com.jjy.pojo.User;
import com.jjy.service.impl.UserServiceImpl;
import com.jjy.tools.validation.Validation;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegisterValidation implements Validation {

  private static RegisterErrorMessage registerErrorMessage = new RegisterErrorMessage();

  @Override
  public boolean doValidation(HttpServletRequest request , UserServiceImpl userServiceImpl) {
    boolean isValid = true;
    String account = request.getParameter("account");
    String password = request.getParameter("password");
    String passwordEnsure = request.getParameter("passwordEnsure");
    String name = request.getParameter("name");
    String gender = request.getParameter("gender");

    String email = request.getParameter("email");
    String hobbies = request.getParameter("hobbies");
    String homeAddress = request.getParameter("homeAddress");
    String job = request.getParameter("job");
    String profilePhoto = request.getParameter("profilePhoto");
    String mobile = request.getParameter("mobile");
    String permission = request.getParameter("permission");

    isValid = LoginValidation.validateAccount(account , registerErrorMessage , isValid);
    User user = new User();
    user.setAccount(account);
    List<User> users = userServiceImpl.selectUserByParams(user);
    if ( users != null && !users.isEmpty()){
      registerErrorMessage.putErrorMessageToJson("account" , EXIST_MESSAGE);
      return false;
    }

    isValid = LoginValidation.validatePassword(password , registerErrorMessage , isValid);
    if(passwordEnsure == null){
      registerErrorMessage.putErrorMessageToJson("passwordEnsure" , NULL_MESSAGE);
      isValid = false;
    } else if ("".equals(passwordEnsure)){
      registerErrorMessage.putErrorMessageToJson("passwordEnsure" , REQUIRED_MESSAGE);
      isValid = false;
    } else if ((!passwordEnsure.equals(password))){
      registerErrorMessage.putErrorMessageToJson("passwordEnsure" , "两次密码不一致,请重新输入!");
      isValid = false;
    }
    if(name == null){
      registerErrorMessage.putErrorMessageToJson("name" , NULL_MESSAGE);
      isValid = false;
    } else if ("".equals(name)){
      registerErrorMessage.putErrorMessageToJson("name" , REQUIRED_MESSAGE);
      isValid = false;
    } else if (name.length() > 30 || name.length() < 3){
      registerErrorMessage.putErrorMessageToJson("name" , FORMAT_MESSAGE + "3到30长度的字母或数字");
      isValid = false;
    }
    if(gender == null){
      registerErrorMessage.putErrorMessageToJson("gender" , NULL_MESSAGE);
      isValid = false;
    } else if ("".equals(gender)){
      registerErrorMessage.putErrorMessageToJson("gender" , REQUIRED_MESSAGE);
      isValid = false;
    } else if (!validateRegex("^[FM0]{1}$" , gender)){
      registerErrorMessage.putErrorMessageToJson("gender" , FORMAT_MESSAGE + "F或M或0");
      isValid = false;
    }
    if(email == null){
      registerErrorMessage.putErrorMessageToJson("email" , NULL_MESSAGE);
      isValid = false;
    } else if (!validateRegex("^[a-zA-Z0-9_-]{3,20}+@[a-zA-Z0-9_-]{2,15}+[\\.][a-zA-Z0-9_-]{2,10}+$" , email)){
      registerErrorMessage.putErrorMessageToJson("email" , FORMAT_MESSAGE + "XXX@XX.XXX");
      isValid = false;
    }
    if(hobbies == null){
      registerErrorMessage.putErrorMessageToJson("hobbies" , NULL_MESSAGE);
      isValid = false;
    } else if (hobbies.length() > 100){
      registerErrorMessage.putErrorMessageToJson("hobbies" , FORMAT_MESSAGE + "100以内长度的字母或数字");
      isValid = false;
    }
    if(homeAddress == null){
      registerErrorMessage.putErrorMessageToJson("homeAddress" , NULL_MESSAGE);
      isValid = false;
    } else if (homeAddress.length() > 50){
      registerErrorMessage.putErrorMessageToJson("homeAddress" , FORMAT_MESSAGE + "50以内的字母或数字");
      isValid = false;
    }
    if(job == null){
      registerErrorMessage.putErrorMessageToJson("job" , NULL_MESSAGE);
      isValid = false;
    } else if (job.length() > 30){
      registerErrorMessage.putErrorMessageToJson("job" , FORMAT_MESSAGE + "30以内长度的字母或数字");
      isValid = false;
    }
    if(mobile == null){
      registerErrorMessage.putErrorMessageToJson("mobile" , NULL_MESSAGE);
      isValid = false;
    } else if (!validateRegex("[0-9]{11,19}" , mobile)){
      registerErrorMessage.putErrorMessageToJson("mobile" , FORMAT_MESSAGE + "18位以内的数字");
      isValid = false;
    }
    if(permission == null){
      registerErrorMessage.putErrorMessageToJson("permission" , NULL_MESSAGE);
      isValid = false;
    } else if (!validateRegex("[0-9]{1}" , permission)){
      registerErrorMessage.putErrorMessageToJson("permission" , FORMAT_MESSAGE + "1或者2");
      isValid = false;
    }
    if(profilePhoto == null){
      registerErrorMessage.putErrorMessageToJson("profilePhoto" , NULL_MESSAGE);
      isValid = false;
    } else if (profilePhoto.length() > 50){
      registerErrorMessage.putErrorMessageToJson("profilePhoto" , FORMAT_MESSAGE + "50以内长度的字母或数字");
      isValid = false;
    }
    return isValid;
  }

  @Override
  public boolean validateRegex(String regex , String str) {
    if(!"".equals(str)){
      Pattern pattern = Pattern.compile(regex);
      Matcher isNum = pattern.matcher(str);
      return isNum.matches();
    }
    return true;
  }

  public RegisterErrorMessage getRegisterErrorMessage() {
    return registerErrorMessage;
  }

}
